package com.wjb.springcloud.pojo;

import com.wjb.config.FeignConfiguration;
import feign.Param;
import feign.RequestLine;
import feign.hystrix.FallbackFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

@Component
@org.springframework.cloud.netflix.feign.FeignClient(name = "user-service",configuration= FeignConfiguration.class,fallbackFactory = FeignClient.HystrixClientFallbackFactor.class)
public interface FeignClient {
//    @RequestMapping(method = RequestMethod.GET,value = "/simple/{id}")
//    Student findByid(@PathVariable(value = "id") Integer id);
    @RequestLine("GET /simple/{id}")
    public Student findById(@Param("id") Integer id);
    @RequestLine("GET /test?id=1")
    public Student findById2();


    @Component
    static class HystrixClientFallbackFactor implements FallbackFactory<FeignClient>{

       private static final Logger logger= LoggerFactory.getLogger(HystrixClientFallbackFactor.class);
        @Override
        public FeignClient create(Throwable throwable) {

            logger.info("--------fallback: reason was:"+throwable.getMessage());
           return new HystrixClientWithFallbackFactory() {
               @Override
               public Student findById2() {
                   Student s=new Student();
                   s.setId(-2L);
                   return s;
               }

               @Override
               public Student findById(Integer id) {
                   Student s=new Student();
                   s.setId(-1L);
                   return s;
               }
           };
        }
    }
//    @Component
//    static class HystrixClientFallback implements FeignClient{
//        @Override
//        public Student findById(@Param("id") Integer id){
//            Student s=new Student();
//            s.setId(0L);
//            return s;
//        }
//    }
}
